package com.zking.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zking.dto.common.ResultDto;
import com.zking.dto.UserDto;
import com.zking.entity.User;
import com.zking.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/api")
public class UserController {
    private final IUserService userService;
    
    @PostMapping("auth/login")
    public ResultDto<UserDto> login(@RequestBody UserDto userDto) {
        User user = userService.getOne(new QueryWrapper<User>()
                .eq("username", userDto.getName())
                .eq("password", userDto.getPassword()));
        if (user == null) {
            return ResultDto.fail(400, "失败");
        }
        UserDto data = new UserDto();
        data.setId(user.getId());
        data.setName(user.getUsername());
        data.setToken(UUID.randomUUID().toString());
        return ResultDto.success(data);
    }
}
